added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBAzureBingMaps / WebRole1 / FederationCallbackHandler.aspx.vb
blobdef3f2e6875cf90c6a113eb3a8693c988f404ecb
1 
2 '***************************** Module Header ******************************\
3 '* Module Name: LoginPage.aspx.vb
4 '* Project: AzureBingMaps
5 '* Copyright (c) Microsoft Corporation.
6 '*
7 '* The callback handler. Configure both ACS and Messenger Connect
8 '* to redirect to this page after the user signs in.
9 '*
10 '* This source is subject to the Microsoft Public License.
11 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 '* All other rights reserved.
13 '*
14 '* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 '* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 '* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 '\**************************************************************************
19 Imports System.Linq
20 Imports System.Net
21 Imports System.ServiceModel.Syndication
22 Imports System.Threading
23 Imports System.Web
24 Imports System.Xml
25 Imports Microsoft.IdentityModel.Claims
27 Partial Public Class FederationCallbackHandler
28 Inherits System.Web.UI.Page
29 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
30 ' Obtain return page from session, and redirect to the page later.
31 Dim returnPage As String = "HtmlClient.aspx"
32 If Session("ReturnPage") IsNot Nothing Then
33 returnPage = DirectCast(Session("ReturnPage"), String)
34 End If
36 ' Parse wl_internalState cookie,
37 ' and extract information for Windows Live Messenger Connect Profile API.
38 ' wl_internalState could be null if the user hasn't tried to login using Live ID.
39 If Response.Cookies("wl_internalState") IsNot Nothing Then
40 Dim accessToken As String = Me.ExtractWindowsLiveInternalState("wl_accessToken")
41 Dim cid As String = Me.ExtractWindowsLiveInternalState("wl_cid")
42 Dim uri As String = "http://apis.live.net/V4.1/cid-" & cid & "/Profiles"
44 ' wl_internalState could be invalid if LiveID login failed.
45 If Not String.IsNullOrEmpty(accessToken) AndAlso Not String.IsNullOrEmpty(cid) Then
47 ' Make a request to profile API.
48 Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
49 request.Headers("Authorization") = accessToken
50 Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
51 If response.StatusCode = HttpStatusCode.OK Then
53 ' Use WCF Syndication API to parse the response.
54 Dim xmlReader As XmlReader = XmlReader.Create(response.GetResponseStream())
55 Dim feed As SyndicationFeed = SyndicationFeed.Load(xmlReader)
56 Dim entry = feed.Items.FirstOrDefault()
57 If entry IsNot Nothing Then
58 Dim content = TryCast(entry.Content, XmlSyndicationContent)
59 If content IsNot Nothing Then
61 ' WindowsLiveProfile is a class
62 ' corresponding to the profile API's response.
63 Dim profile = content.ReadContent(Of WindowsLiveProfile)()
64 Dim liveID = profile.Emails.Where(Function(m) String.Equals(m.Type, "WindowsLiveID")).FirstOrDefault()
66 ' If profile API succeeds,
67 ' we'll be able to obtain the user's LiveID.
68 ' The LiveID will be the user's identity.
69 ' We store user identity in session.
70 If liveID IsNot Nothing Then
71 Session("User") = liveID.Address
72 End If
73 End If
74 End If
75 xmlReader.Close()
76 End If
77 End If
78 End If
80 ' The following code deals with ACS via WIF.
81 Dim principal = TryCast(Thread.CurrentPrincipal, IClaimsPrincipal)
82 If principal IsNot Nothing AndAlso principal.Identities.Count > 0 Then
83 Dim identity = principal.Identities(0)
85 ' Query for email claim.
86 Dim query = From c In identity.Claims Where c.ClaimType = ClaimTypes.Email Select c
87 Dim emailClaim = query.FirstOrDefault()
88 If emailClaim IsNot Nothing Then
90 ' Store user identity in session.
91 Session("User") = emailClaim.Value
92 End If
93 End If
94 ' Redirect user to the return page.
95 Response.Redirect(returnPage)
96 End Sub
98 ''' <summary>
99 ''' Extract information for Windows Live Messenger Connect Profile API
100 ''' from wl_internalState cookie.
101 ''' The cookie contains a bunch of information
102 ''' such as cid and access token.
103 ''' </summary>
104 ''' <param name="key">Which data to extract.</param>
105 ''' <returns>The value of the data.</returns>
106 Private Function ExtractWindowsLiveInternalState(ByVal key As String) As String
107 Dim result As String = Request.Cookies("wl_internalState").Value
109 result = HttpUtility.UrlDecode(result)
110 result = result.Substring(result.IndexOf(key))
111 result = result.Substring(key.Length + 3, result.IndexOf(","c) - key.Length - 4)
113 ' wl_internalState could be invalid if LiveID login failed.
114 ' In this case, we return null.
115 Catch
116 result = Nothing
117 End Try
118 Return result
119 End Function
120 End Class